package smart.src;

import java.sql.SQLException;

import org.openswing.swing.form.client.FormController;
import org.openswing.swing.mdi.client.InternalFrame;
import org.openswing.swing.mdi.client.MDIFrame;
import org.openswing.swing.message.receive.java.ErrorResponse;
import org.openswing.swing.message.receive.java.Response;
import org.openswing.swing.message.receive.java.VOResponse;
import org.openswing.swing.message.receive.java.ValueObject;
import org.openswing.swing.util.java.Consts;

import src.DAO.ClientDaoImpl;
import src.VO.ClientVO;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.support.ConnectionSource;

public class ClientDetailFrameController extends FormController {
	
	ConnectionSource connectionSource; 
	private ClientDaoImpl clientDao;
	private ClientDetailFrame frame;
	
	public ClientDetailFrameController(ConnectionSource connectionSource) {
		this.connectionSource = connectionSource;
		frame = new ClientDetailFrame(connectionSource, this);
		MDIFrame.add(frame);
		setUpDao();
		frame.getClientForm().setMode(Consts.READONLY);
		frame.getClientForm().reload();
	}

	/* (non-Javadoc)
	 * @see org.openswing.swing.form.client.FormController#insertRecord(org.openswing.swing.message.receive.java.ValueObject)
	 */
	@Override
	public Response insertRecord(ValueObject newPersistentObject)
			throws Exception {
		ClientVO vo = (ClientVO)newPersistentObject;
		clientDao.create(vo);
		return new VOResponse(vo);
	}

	/* (non-Javadoc)
	 * @see org.openswing.swing.form.client.FormController#loadData(java.lang.Class)
	 */
	@Override
	public Response loadData(Class valueObjectClass) {
		Integer id = 1;
		ClientVO vo = clientDao.getById(id);
		System.out.println("Load data:"+vo);
		if(vo != null)
			return new VOResponse(vo);
		else {
			return new ErrorResponse("Client with id="+id+" load failed");
		}
	}
	
	
	private void setUpDao()
	{
		try {
			clientDao = new ClientDaoImpl(connectionSource);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


}
